<?php

require_once 'classes/Placement.php';
require_once 'SCinterface.php';
require_once 'classes/Message.php';

/*
 * Classe de service CRUD pour manipuler les enregistrements de Placements
 * @author Zikrija
 * @date 04.05.2012
 */
class SCplacement {
    
    private $db;

    /**
     *Contructeur créant l'objet pour la connexion à la DB 
     */
    public function __construct() {
        $this->db = new PDO(ACA_DB_DSN, ACA_DB_USER, ACA_DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    /**
    * Ajoute un Placement
    * @param Placement Placement à ajouter
    */
    public function ajouter($unPlacementAAjouter) {
        try{

            $query =  $this->db->prepare("INSERT INTO placement " .
                "(id, dateArrivee, dateDepart, montantParJour, veterinairePrive, supplement, datePaiement, contrat_id) VALUES(".
                ":id, :dateArrivee, :dateDepart, :montantParJour, :veterinairePrive, :supplement, :datePaiement, :contrat_id)");

            $query->execute(array(
                'id'               => $unPlacementAAjouter->getId() ,
                'dateArrivee'      => $unPlacementAAjouter->getDateArrivee(),
                'dateDepart'       => $unPlacementAAjouter->getDateDepart() ,
                'montantParJour'   => $unPlacementAAjouter->getMontantParJour() ,
                'veterinairePrive' => $unPlacementAAjouter->getVeterinairePrive(),
                'supplement'       => $unPlacementAAjouter->getSupplement(),
                'datePaiement'     => $unPlacementAAjouter->getDatePaiement(),
                'contrat_id'       => $unPlacementAAjouter->getContrat_id()    
                ));
            $unPlacementAAjouter->setId($this->db->lastInsertId());
            $query->closeCursor();
        }
        catch(PDOException $e){
            return new Message("Placement non ajouté: ".$e->getMessage());
        }
        return $unPlacementAAjouter;
   }
    /**
     * Methode permettant de modifier un placement
     * @param Placement Placement à modifier
     * @return boolean 
     */
    public function modifier($unPlacementAModifier){
        try{

            $query =  $this->db->prepare("UPDATE placement SET ".
                "dateArrivee = :dateArrivee, dateDepart = :dateDepart , montantParJour = :montantParJour, ".
                "veterinairePrive = :veterinairePrive, supplement = :supplement, datePaiement = :datePaiement, contrat_id = :contrat_id ".
                "WHERE id = :id");

            $query->execute(array(
                'id'               => $unPlacementAModifier->getId() ,
                'dateArrivee'      => $unPlacementAModifier->getDateArrivee(),
                'dateDepart'       => $unPlacementAModifier->getDateDepart() ,
                'montantParJour'   => $unPlacementAModifier->getMontantParJour() ,
                'veterinairePrive' => $unPlacementAModifier->getVeterinairePrive(),
                'supplement'       => $unPlacementAModifier->getSupplement(),
                'datePaiement'     => $unPlacementAModifier->getDatePaiement(),
                'contrat_id'       => $unPlacementAModifier->getContrat_id()
            ));

            $query->closeCursor();


        }
        catch(PDOException $e){
            return new Message("Placement non modifié: ".$e->getMessage());
        }
        return $unPlacementAModifier;
    }
    /**
     * Methode permettant de supprimer un placement
     * @param int id du Placement à supprimer
     * @return boolean 
     */
    public function supprimer($idPlacementASupprimer){
        try{
            $query = $this->db->prepare("DELETE FROM placement WHERE id = :id");
            $query->execute(array(
                'id'           => $idPlacementASupprimer
            ));            
            $query->closeCursor();
            
            $message = new Message("Placement supprimé"); 
        }
        catch(PDOException $e){
            $message = new Message("Placement non supprimé: ". $e->getMessage());
        }
        return $message;
    }
   
    /**
     * 
     * @param Placement de recherche sous forme d'objet Placement
     * @return un tableau d'objet Placement
     */
    public function lister($PlacementALister){
        try{
            $sql="SELECT * FROM placement ";
            $sql_Where="WHERE";
            if($PlacementALister!=NULL)
            {
                if($PlacementALister->getId()!=NULL)
                    $sql_Where.=" id = '".$PlacementALister->getId()."' AND ";
                if($PlacementALister->getDateArrivee()!=NULL)
                    $sql_Where.=" dateArrivee = '".$PlacementALister->getDateArrivee()."' AND ";
                if($PlacementALister->getDateDepart()!=NULL)
                    $sql_Where.=" dateDepart = '".$PlacementALister->getDateDepart()."' AND ";
                if($PlacementALister->getMontantParJour()!=NULL)
                    $sql_Where.=" montantParJour = '".$PlacementALister->getMontantParJour()."' AND ";
                if($PlacementALister->getVeterinairePrive()!=NULL)
                    $sql_Where.=" veterinairePrive = '".$PlacementALister->getVeterinairePrive()."' AND ";
                if($PlacementALister->getSupplement()!=NULL)
                    $sql_Where.=" supplement = '".$PlacementALister->getSupplement()."' AND ";
                if($PlacementALister->getDatePaiement()!=NULL)
                    $sql_Where.=" datePaiement = '".$PlacementALister->getDatePaiement()."' AND ";
                if($PlacementALister->getContrat_id()!=NULL)
                    $sql_Where.=" contrat_id = '".$PlacementALister->getContrat_id()."' AND ";
            }         

            $sql_Where = substr($sql_Where,0,-5);
            $sql.=$sql_Where." ORDER BY id";
            $lesPlacementsTrouves=NULL;
            $unPlacementLu=NULL;
            $result=$this->db->query($sql);
            while($row = $result->fetch())
            {   
                $unPlacementLu = new Placement($row['id'], $row['dateArrivee'], $row['dateDepart'], $row['montantParJour'], $row['veterinairePrive'], $row['supplement'], $row['datePaiement'], $row['contrat_id']);

                $lesPlacementsTrouves[]=$unPlacementLu;
            }                
        }
        catch(PDOException $e)
        {
            print($e->getMessage());
            $lesPlacementsTrouves=NULL;
        }
        return $lesPlacementsTrouves;
    } 
}

?>
